<?php
include "../../inc/log/adsession.php";
?>
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<?php
	require '../../vendor/autoload.php';
	include "../../inc/data/connAdmin.php";
	
	// 1903010328引入资源
	use PhpOffice\PhpSpreadsheet\IOFactory;

	//  1903010328实例化phpspreadsheet对象
	$file = $_FILES["classfile"]["tmp_name"];
	$spreadSheet = IOFactory::load($file);

	// 1903010328获取表
	$workSheet = $spreadSheet->getSheet(0);
	// 1903010328最大行数
	$rows = $workSheet->getHighestRow();
	// 1903010328获取动态的专业名
	$majorName = $_POST['major'];
	for ($i=2; $i <= $rows; $i++) { 
		$sql = "insert into mamx_class 
				(classname,classyear,majorname) values 
				(?, ?,'$majorName')";
		if($stmt=$conn->prepare($sql)){
			// 1903010328获取专业名称,判断是否为空
			$classname = $workSheet->getCell("a$i")->getValue();
			// 查看是否是有效值
			if ($classname == ""){
				break;
			}
			// 1903010328对获取到的学制进行赋值
			$classyear = $workSheet->getCell("b$i")->getValue();
			$stmt->bind_param("ss",$classname,$classyear);
			$stmt->execute();
			if ($stmt->affected_rows > 0) {
				$count += 1;
			}
		}
	}
	if ($count > 0){
		//  1903010328录入成功弹出提示框提示
		echo <<<END
	    <script type="text/javascript">
	    alert("已成功在 $majorName 专业中 添加 $count 个班级！");
	    history.back();
	    </script>
END;
	}else{
		//  录入失败弹出提示框提示
		?>
		<script type="text/javascript">
	    alert("添加失败，请重试！");
	    history.back();
	    </script>
	    <?php
	}
	?>
</body>
</html>